<?php
//online - updates users_online database
function online() {
	global $conf;

	$db = new dbquery;
	if(!$GLOBALS['online']){
		$time = time();

		if(!$_SESSION['logged_in'])
		$dane=0;
		else
		$dane=$_SESSION['id'];

		if(($time-$_SESSION['online_time']>=$conf['online_time']) or $time==$_SESSION['online_time']){
			$timeout=$_SESSION['online_time']-$conf['online_time'];
			$db->query("DELETE FROM $conf[prefix]online WHERE time<='$timeout' or sid='".session_id()."'") or $db->err(__FILE__, __LINE__);
			$db->query("INSERT INTO $conf[prefix]online VALUES (".time().",'$_SERVER[REMOTE_ADDR]','$_SERVER[SCRIPT_NAME]?$_SERVER[QUERY_STRING]', '".session_id()."', '$dane', '".$_SESSION['lang_short']."', '".$_SERVER['HTTP_USER_AGENT']."', '".$_SERVER['HTTP_ACCEPT_LANGUAGE']."')") or $db->err(__FILE__, __LINE__);
			$_SESSION['online_time']=$time;
		}
		else
		$db->query("UPDATE $conf[prefix]online SET time='$time', file='$_SERVER[SCRIPT_NAME]?$_SERVER[QUERY_STRING]', lang='".$_SESSION['lang_short']."' WHERE sid='".session_id()."'") or $db->err(__FILE__, __LINE__);

		$GLOBALS['online']=true;
	}
}

//returns online guests (number)
function online_guests($_URI=NULL)
{
	global $conf;

	online();
  
    if($_URI)
		$sql=" && file LIKE '%$_URI%'";
  
	$db = new dbquery;
	$db->query("SELECT COUNT(DISTINCT sid) as row FROM $conf[prefix]online WHERE logged_in='0'".$sql) or $db->err(__FILE__, __LINE__);
	$d=$db->fetch_object();

	return $d->row;
}
//

//quantity of logged users
function online_logged($_URI=NULL)
{
	global $conf;

	online();

	if($_URI)
		$sql=" && file LIKE '%$_URI%'";

	$db = new dbquery;
	$db->query("SELECT COUNT(DISTINCT sid) as row FROM $conf[prefix]online WHERE logged_in!='0'".$sql) or $db->err(__FILE__, __LINE__);
	$d=$db->fetch_object();

	return $d->row;
}
//

function online_logged_nick($prefix, $spacer=', ', $class=NULL, $page=NULL, $gid=NULL)
{
	global $conf;

	online();

	$db = new dbquery;
	if(!is_numeric($gid))
		$db->query("SELECT logged_in FROM $conf[prefix]online WHERE logged_in!='0' && file LIKE '%$page%'") or $db->err(__FILE__, __LINE__);
	else
		$db->query("SELECT $conf[prefix]online.logged_in FROM $conf[prefix]online, $conf[prefix]users WHERE $conf[prefix]online.logged_in!='0' && $conf[prefix]online.file LIKE '%$page%' && $conf[prefix]users.gid=$gid && $conf[prefix]online.logged_in=$conf[prefix]users.id") or $db->err(__FILE__, __LINE__);

	$text=array();
	while($d=$db->fetch_object())
		$text[]=get_user_overlib_by_id($d->logged_in);
	$text='<span class="'.$class.'">'.$prefix.implode($spacer, $text).'</span>';

	return $text;
}

//return true if user with $id_ is logged in, else return false
function is_online($id_)
{
	global $conf;
	
	SQLvalidate($id_);
	
	$db = new dbquery;

	if($id_)
	{
		$db->query("SELECT logged_in FROM $conf[prefix]online WHERE logged_in=$id_") or $db->err(__FILE__, __LINE__);
		$count=$db->num_rows();

		if($count == 0)
		return false;
		else
		return true;
	}
	else
	return false;
}

//add records to stats
function  stats($online=false)
{
	global $conf;
	
	//zabezpieczenie przed botami
	$a=array('crawl', 'bot', 'search');
	$browser=$_SERVER['HTTP_USER_AGENT'];

	if(!$browser)
		return false;		
	for($i=0;$i<count($a);$i++){
		if(strpos($browser, $a[$i])!==false)
			return false;
	}

	if(!$_SESSION['visit']) {
		add_uni();
		add_raw();
		$_SESSION['visit']='visited';
	}
	else
		add_raw();

	if($online)
		online();
}
//

function  add_uni()
{
	global $conf, $lang;

	$db = new dbquery;

	$date=date('Y-m-d');
	$db->cache($date, 'stats');
	$db->query("SELECT COUNT(date) as row FROM $conf[prefix]stats WHERE date='$date'") or $db->err(__FILE__, __LINE__);
	$d=$db->fetch_object(true);
	
	//dodanie uni
	if($d->row==0){
		$db->query("INSERT INTO $conf[prefix]stats VALUES('$date', '1', '0')") or $db->err(__FILE__, __LINE__);
		$db->clear_cache($date.'.cache', 'stats');
		$db->clear_cache(date('Y-m-d', strtotime("-1 day")).'.cache', 'stats');
	}
	else
	$db->query("UPDATE $conf[prefix]stats SET uni=uni+1 WHERE date='$date' or date='0000-00-00'") or $db->err(__FILE__, __LINE__);
	//
}

function  add_raw()
{
	global $conf, $lang;

	$db = new dbquery;

	$date=date('Y-m-d');
	$db->cache($date, 'stats');
	$db->query("SELECT COUNT(date) as row FROM $conf[prefix]stats WHERE date='$date'") or $db->err(__FILE__, __LINE__);
	$d=$db->fetch_object(true);

	//dodanie raw
	if($d->row==0){
		$db->query("INSERT INTO $conf[prefix]stats VALUES('$date', '0', '1')");
		$db->clear_cache($date.'.cache', 'stats');
		$db->clear_cache(date('Y-m-d', strtotime("-1 day")), 'stats');
	}
	else
	$db->query("UPDATE $conf[prefix]stats SET raw=raw+1 WHERE date='$date' or date='0000-00-00'") or $db->err(__FILE__, __LINE__);
	//
}

function  percent($co, $przez_co) {
	return @round(($co / $przez_co)*100, 0).'%';
}

function  all_all($date = NULL, $start = 0) {
	global $conf, $lang;

	$db = new dbquery;

	if(!$date)
	$zapytanie = "SELECT uni, raw FROM $conf[prefix]stats";
	else
	$zapytanie = "SELECT * FROM $conf[prefix]stats WHERE date = '$date'";

	$db->query($zapytanie) or $db->err(__FILE__, __LINE__);

	while($r=$db->fetch_object())
	$return+=$r->uni+$r->raw;

	return $return+$start;
}

function  all_uni($date = NULL) {
	global $conf, $lang;

	$db = new dbquery;

	if(!$date)
	$zapytanie = "SELECT `uni` FROM $conf[prefix]stats";
	else
	$zapytanie = "SELECT `uni` FROM $conf[prefix]stats WHERE date = '$date'";
	$db->query($zapytanie) or $db->err(__FILE__, __LINE__);

	while($r=$db->fetch_object())
	$return+=$r->uni;

	return $return;
}

function  all_raw($date = NULL) {
	global $conf, $lang;

	$db = new dbquery;

	if(!$date)
	$zapytanie = "SELECT `raw` FROM $conf[prefix]stats";
	else
	$zapytanie = "SELECT `raw` FROM $conf[prefix]stats WHERE date = '$date'";

	$db->query($zapytanie) or $db->err(__FILE__, __LINE__);

	while($r=$db->fetch_object())
	$return+=$r->raw;

	return $return;
}

function stats_image() {
	global $thm_img, $conf;
	
	$GLOBALS['theme_path']='../themes/'.$_SESSION['theme'].'/';
	include_once('functions/stats-external.php');
	include_once($GLOBALS['theme_path'].'gfx/images.php');

	header("Content-type: image/jpeg"); 
	$img = imagecreatefromjpeg($GLOBALS['theme_path'].$GLOBALS['theme_img']['stats_image']); 

	$textcolor = imagecolorallocate($img, $conf['code_img_tx_'.$_SESSION['theme']]['red'], $conf['code_img_tx_'.$_SESSION['theme']]['green'], $conf['code_img_tx_'.$_SESSION['theme']]['blue']); 
	if(!$_GET['today'])
	{
	  $raw=all_raw();
	  $uni=all_uni();
	  $sum=$raw+$uni;
	  imagestring($img, 3, 7, 1, 'RAW '.$raw, $textcolor);
	  imagestring($img, 3, 7, 13, 'UNI '.$uni, $textcolor);
	  imagestring($img, 3, 7, 25, 'ALL '.$sum, $textcolor);  
	}
	else
	{ 
	  $date=date('Y-m-d');
	  $raw=all_raw($date);
	  $uni=all_uni($date);
	  $sum=$raw+$uni;
	  imagestring($img, 3, 7, 1, 'RAW '.$raw, $textcolor);
	  imagestring($img, 3, 7, 13, 'UNI '.$uni, $textcolor);
	  imagestring($img, 3, 7, 25, 'ALL '.$sum, $textcolor);  
	} 
	imagejpeg($img); 
	imagedestroy($img);
}

//kraje - sprawdza czy dana ko�c�wka hosta ma odpowiednik w krajach, je�eli tak to zwraca nazwe kraju je�eli nie to zwraca false
function countries($what, $ext)
{
	//baza kraj�w 111
	$country['AO']='ANGOLA';
	$country['AG']='ANTIGUA AND BARBUDA';
	$country['AR']='ARGENTINA';
	$country['AM']='ARMENIA';
	$country['AU']='AUSTRALIA';
	$country['AT']='AUSTRIA';
	$country['BS']='BAHAMAS';
	$country['BH']='BAHRAIN';
	$country['BD']='BANGLADESH';
	$country['BB']='BARBADOS';
	$country['BY']='BELARUS';
	$country['BE']='BELGIUM';
	$country['BM']='BERMUDA';
	$country['BO']='BOLIVIA';
	$country['BR']='BRAZIL';
	$country['BN']='BRUNEI DARUSSALAM';
	$country['CA']='CANADA';
	$country['CL']='CHILE';
	$country['CN']='CHINA';
	$country['CO']='COLOMBIA';
	$country['CR']='COSTA RICA';
	$country['HR']='CROATIA';
	$country['CU']='CUBA';
	$country['CY']='CYPRUS';
	$country['CZ']='CZECH REPUBLIC';
	$country['DK']='DENMARK';
	$country['DO']='DOMINICAN REPUBLIC';
	$country['EC']='ECUADOR';
	$country['EG']='EGYPT';
	$country['EE']='ESTONIA';
	$country['FO']='FAROE ISLANDS';
	$country['FI']='FINLAND';
	$country['FR']='FRANCE';
	$country['DE']='GERMANY';
	$country['GR']='GREECE';
	$country['GU']='GUAM';
	$country['GT']='GUATEMALA';
	$country['HN']='HONDURAS';
	$country['HK']='HONG KONG';
	$country['HU']='HUNGARY';
	$country['IS']='ICELAND';
	$country['IN']='INDIA';
	$country['ID']='INDONESIA';
	$country['IR']='IRAN';
	$country['IQ']='IRAQ';
	$country['IE']='IRELAND';
	$country['IL']='ISRAEL';
	$country['IT']='ITALY';
	$country['JM']='JAMAICA';
	$country['JP']='JAPAN';
	$country['JO']='JORDAN';
	$country['KZ']='KAZAKHSTAN';
	$country['KE']='KENYA';
	$country['KR']='KOREA';
	$country['KW']='KUWAIT';
	$country['LV']='LATVIA';
	$country['LB']='LEBANON';
	$country['LT']='LITHUANIA';
	$country['MO']='MACAU';
	$country['MW']='MALAWI';
	$country['MY']='MALAYSIA';
	$country['MT']='MALTA';
	$country['MU']='MAURITIUS';
	$country['MX']='MEXICO';
	$country['MA']='MOROCCO';
	$country['MZ']='MOZAMBIQUE';
	$country['NA']='NAMIBIA';
	$country['NL']='NETHERLANDS';
	$country['NZ']='NEW ZEALAND';
	$country['NI']='NICARAGUA';
	$country['NG']='NIGERIA';
	$country['MP']='NORTHERN MARIANA ISLANDS';
	$country['NO']='NORWAY';
	$country['PK']='PAKISTAN';
	$country['PA']='PANAMA';
	$country['PY']='PARAGUAY';
	$country['PE']='PERU';
	$country['PH']='PHILIPPINES';
	$country['PL']='POLAND';
	$country['PT']='PORTUGAL';
	$country['PR']='PUERTO RICO';
	$country['QA']='QATAR';
	$country['RO']='ROMANIA';
	$country['RU']='RUSSIAN FEDERATION';
	$country['VC']='SAINT VINCENT AND THE GRENADINES';
	$country['SA']='SAUDI ARABIA';
	$country['SG']='SINGAPORE';
	$country['SK']='SLOVAKIA';
	$country['SI']='SLOVENIA';
	$country['SB']='SOLOMON ISLANDS';
	$country['ZA']='SOUTH AFRICA';
	$country['ES']='SPAIN';
	$country['LK']='SRI LANKA';
	$country['SR']='SURINAME';
	$country['SE']='SWEDEN';
	$country['CH']='SWITZERLAND';
	$country['TW']='TAIWAN';
	$country['TH']='THAILAND';
	$country['TT']='TRINIDAD AND TOBAGO';
	$country['TR']='TURKEY';
	$country['UG']='UGANDA';
	$country['UA']='UKRAINE';
	$country['AE']='UNITED ARAB EMIRATES';
	$country['GB']='UNITED KINGDOM';
	$country['US']='UNITED STATES';
	$country['UY']='URUGUAY';
	$country['VE']='VENEZUELA';
	$country['VI']='VIRGIN ISLANDS (U.S.)';
	$country['YU']='YUGOSLAVIA';
	$country['ZM']='ZAMBIA';
	$country['ZW']='ZIMBABWE';
	//
	if($what=='country')
	{
		if($country[strtoupper($ext)])
		return $country[strtoupper($ext)];
		else
		return false;
	}
	else
	{
		if($country[strtoupper($ext)])
		return strtoupper($ext);
		else
		return false;
	}
}
//

?>
